package com.yuandengta.yomall.order.web;

import com.yuandengta.yomall.order.service.AliPayService;
import com.yuandengta.yomall.order.vo.PayAsyncVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单支付 {@link OrderPayController}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@RestController
public class OrderPayController {

    @Autowired
    private AliPayService aliPayService;

    /**
     * 跳转到支付宝支付页
     * @param orderSn
     * @return
     */
    @GetMapping(value = "/payOrder", produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn){
        // 返回的是支付宝收银页面
        String pay = aliPayService.pay(orderSn);

        return pay;
    }

    /**
     * 支付宝支付成功异步通知
     * @param vo
     * @return
     */
    @PostMapping("/alipay/notify")
    public String handleAlipay(PayAsyncVO vo, HttpServletRequest request){
        String result = null;
        try {
            result = aliPayService.payNotify(vo, request);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
